package com.thrd.taiexhibition.ui;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.thrd.taiexhibition.bean.BeanCityIndustryDetail;
import com.thrd.taiexhibition.fresco.FrescoImage;
import com.thrd.taiexhibition.fresco.FrescoUtils;

import java.util.List;

/**
 * @author: ${唐杰}
 * @date: 2021/3/25
 */
public class FocusIndustryInfoView extends FrameLayout {

    private Paint p;

    public FocusIndustryInfoView(@NonNull Context context) {
        super(context);
    }

    public FocusIndustryInfoView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FocusIndustryInfoView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }




    private List<BeanCityIndustryDetail> beanCityIndustryDetailsList;
    public  void setTouchData(String pathImage, List<BeanCityIndustryDetail>  beanCityIndustryDetailsList){
        this.beanCityIndustryDetailsList=beanCityIndustryDetailsList;
        addImageView(beanCityIndustryDetailsList);
       // invalidate();
      //  setControllerListener(FocusIndustryInfoView.this, Constant.IMAGE+pathImage);

    }

    private void addImageView(List<BeanCityIndustryDetail>  beanCityIndustryDetailsList) {
        removeAllViews();
        if(beanCityIndustryDetailsList!=null){
            for (BeanCityIndustryDetail mBeanCityIndustryDetail:beanCityIndustryDetailsList){
                FrescoImage mFrescoImage=new FrescoImage(getContext());
                FrameLayout.LayoutParams mLayoutParams=new FrameLayout.LayoutParams(mBeanCityIndustryDetail.getWidth(),mBeanCityIndustryDetail.getLength());
                mLayoutParams.topMargin=mBeanCityIndustryDetail.getyAxis();
                mLayoutParams.leftMargin=mBeanCityIndustryDetail.getxAxis();
                mFrescoImage.setLayoutParams(mLayoutParams);
                mFrescoImage.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if(onTouchImageListener!=null){
                            onTouchImageListener.onTouchImageListener(mBeanCityIndustryDetail);
                        }

                    }
                });
                FrescoUtils.getInstance().setPrefixProgressLoad(getContext(),mFrescoImage,mBeanCityIndustryDetail.getIndustryDetailIcon());
                addView(mFrescoImage);
            }

        }

    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 创建画笔
        if(p==null){
            p = new Paint();
            p.setColor(Color.RED);// 设置红色
            p.setAntiAlias(true);// 设置画笔的锯齿效果。 true是去除，大家一看效果就明白了
            //画笑脸弧线
            p.setStyle(Paint.Style.STROKE);//设置空心
            p.setStrokeWidth(8);//这个地方改线宽,单位是像素
        }
       if(beanCityIndustryDetailsList!=null){
           for (int i=0;i<beanCityIndustryDetailsList.size();i++){
               BeanCityIndustryDetail beanCityIndustryDetail= beanCityIndustryDetailsList.get(i);
               if(beanCityIndustryDetail!=null){
                   canvas.drawRect(beanCityIndustryDetail.getxAxis()
                           , beanCityIndustryDetail.getyAxis()
                           , beanCityIndustryDetail.getxAxis()+beanCityIndustryDetail.getWidth()
                           , beanCityIndustryDetail.getyAxis()+beanCityIndustryDetail.getLength(), p);// 长方形
               }

           }
       }


    }

//    @Override
//    public boolean dispatchTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                touchX = (int) event.getX();
//                touchY = (int) event.getY();
//                LogUtil.e("onTouchEvent","DOWN");
//                break;
//
//            case MotionEvent.ACTION_MOVE:
//                LogUtil.e("onTouchEvent","MOVE");
//                break;
//            case MotionEvent.ACTION_UP:
//                LogUtil.e("onTouchEvent","UP");
//                int x = (int) event.getX();
//                int y = (int) event.getY();
//                LogUtil.e("onTouchEvent","x="+x+"y="+y);
//                if ((Math.abs(x - touchX) < 30) && (Math.abs(y - touchY) < 30)&&onTouchImageListener!=null) {
//                    LogUtil.e("onTouchEvent","是点击事件");
//                    for (BeanCityIndustryDetail mCoordinate :beanCityIndustryDetailsList){
//                        if(isArea(x,y,mCoordinate)){
//                            LogUtil.e("onTouchEvent","是点击位置");
//                            onTouchImageListener.onTouchImageListener(mCoordinate);
//                            break;
//                        }
//                    }
//
//
//                }else{
//                    LogUtil.e("onTouchEvent","<30");
//                }
//
//                break;
//
//
//            default:
//                break;
//        }
//        return true;
//
//    }
//
//    private int touchX;
//    private int touchY;
//    @Override
//    public boolean onTouchEvent(MotionEvent event) {
//        switch (event.getAction()) {
//            case MotionEvent.ACTION_DOWN:
//                touchX = (int) event.getX();
//                touchY = (int) event.getY();
//                LogUtil.e("onTouchEvent","DOWN");
//                break;
//
//            case MotionEvent.ACTION_MOVE:
//                LogUtil.e("onTouchEvent","MOVE");
//                break;
//            case MotionEvent.ACTION_UP:
//                LogUtil.e("onTouchEvent","UP");
//                int x = (int) event.getX();
//                int y = (int) event.getY();
//                LogUtil.e("onTouchEvent","x="+x+"y="+y);
//                if ((Math.abs(x - touchX) < 30) && (Math.abs(y - touchY) < 30)&&onTouchImageListener!=null) {
//                    LogUtil.e("onTouchEvent","是点击事件");
//                    for (BeanCityIndustryDetail mCoordinate :beanCityIndustryDetailsList){
//                        if(isArea(x,y,mCoordinate)){
//                            LogUtil.e("onTouchEvent","是点击位置");
//                            onTouchImageListener.onTouchImageListener(mCoordinate);
//                            break;
//                        }
//                    }
//
//
//                }else{
//                    LogUtil.e("onTouchEvent","<30");
//                }
//
//                break;
//
//
//            default:
//                break;
//        }
//
//        return true;
//    }

//    private boolean isArea(float x,float y,BeanCityIndustryDetail mCoordinate){
//        int width=mCoordinate.getWidth()/2;
//        int hight=mCoordinate.getLength()/2;
//        if(Math.abs(x- (mCoordinate.getxAxis()+width))<width&&Math.abs(y- (mCoordinate.getyAxis()+hight))<hight){
//            return true;
//        }
//        return false;
//    }



    private   void setControllerListener(final ImageView mImageView, String imagePath) {
//        OkHttpUtils.get().url(imagePath).build().execute(new BitmapCallback() {
//            @Override
//            public void onError(Call call, Exception e, int id) {
//                LogUtil.e("Image","onError");
//            }
//
//            @Override
//            public void onResponse(Bitmap response, int id) {
//                LogUtil.e("Image","onError");
//                mImageView.setImageBitmap(response);
//            }
//
//            @Override
//            public Bitmap onJson(String json) throws Exception {
//                return null;
//            }
//
//
//        });
    }

    private OnTouchImageListener onTouchImageListener;

    public OnTouchImageListener getOnTouchImageListener() {
        return onTouchImageListener;
    }

    public void setOnTouchImageListener(OnTouchImageListener onTouchImageListener) {
        this.onTouchImageListener = onTouchImageListener;
    }

    public interface OnTouchImageListener{

        void onTouchImageListener(BeanCityIndustryDetail mBeanCityIndustryDetail);
    }


}
